package com.aki.akiojcodesandbox.utils;

import com.aki.akiojcodesandbox.model.ExecuteMessage;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.util.StopWatch;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ProcessUtils {

    public static ExecuteMessage runProcessAndGetMessage(Process runProcess, String opName){
        ExecuteMessage executeMessage = new ExecuteMessage();
        try {
            //计时开始
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            //Process process = Runtime.getRuntime().exec(compileCmd);
            int exitValue = runProcess.waitFor();//这里获取程序运行结果 码
            executeMessage.setExitValue(exitValue);
            if(exitValue==0){//错误码为0则成功
                System.out.println(opName+"成功");
                //获取控制台信息,这里是输入流里获取
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(runProcess.getInputStream()));
                String compileOutputLine;
                StringBuilder stringBuilder = new StringBuilder() ;
                while((compileOutputLine=bufferedReader.readLine())!=null){
                    stringBuilder.append(compileOutputLine);
                }
                System.out.println(stringBuilder);
                executeMessage.setMessage(stringBuilder.toString());
            }else{
                //异常退出
                System.out.println(opName+"失败，错误码："+exitValue);
                //获取控制台信息,输出异常信息  分批获取进程的输出
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(runProcess.getInputStream()));
                String compileOutputLine;
                StringBuilder stringBuilder = new StringBuilder() ;
                while((compileOutputLine=bufferedReader.readLine())!=null){
                    stringBuilder.append(compileOutputLine);
                }
                System.out.println(stringBuilder);
                executeMessage.setMessage(stringBuilder.toString());
                stringBuilder=new StringBuilder();
                // 分批获取进程的错误输出
                BufferedReader errorBufferedReader = new BufferedReader(new InputStreamReader(runProcess.getErrorStream()));
                // 逐行读取
                //List<String> errorOutputStrList = new ArrayList<>();
                // 逐行读取
                String errorCompileOutputLine;
                while ((errorCompileOutputLine = errorBufferedReader.readLine()) != null) {
                    stringBuilder.append(errorCompileOutputLine);
                }
                executeMessage.setErrorMessage(stringBuilder.toString());//分隔符
            }
            stopWatch.stop();
            //计时结束
            executeMessage.setTime(stopWatch.getLastTaskTimeMillis());
        } catch (InterruptedException | IOException e) {
            throw new RuntimeException(e);
        }

        return executeMessage;
    }

}
